//
//  PictureTableViewCell.m
//  项目A_百思不得姐
//
//  Created by wudongyang on 16/5/18.
//  Copyright © 2016年 wudongyang. All rights reserved.
//

#import "PictureTableViewCell.h"
#import "UIImageView+WebCache.h"
#import "tagsModel.h"

@implementation PictureTableViewCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self createSubViews];
    }
    return self;
}
-(void)createSubViews{
    
    self.HeadImageV = [[UIImageView alloc] init];
    self.HeadImageV.backgroundColor = [UIColor blueColor];
    //给边框加弧度
    self.HeadImageV.layer.masksToBounds = YES;
    self.HeadImageV.layer.cornerRadius = 15;
    [self.contentView addSubview:self.HeadImageV];
    
    self.NameLabel = [[UILabel alloc] init];
    self.NameLabel.backgroundColor = [UIColor clearColor];
    self.NameLabel.font = [UIFont systemFontOfSize:13];
    self.NameLabel.textColor = DSRGBA(124, 124, 124, 1);
    [self.contentView addSubview:self.NameLabel];
    
    self.TimeLabel = [[UILabel alloc] init];
    self.TimeLabel.backgroundColor = [UIColor clearColor];
    self.TimeLabel.textColor = DSRGBA(124, 124, 124, 1);
    self.TimeLabel.font = [UIFont systemFontOfSize:13];
    [self.contentView addSubview:self.TimeLabel];
    
    self.TitleLabel = [[UILabel alloc] init];
    self.TitleLabel.backgroundColor = [UIColor clearColor];
    [self.contentView addSubview:self.TitleLabel];
    // 文字自动换行
     self.TitleLabel.numberOfLines = 0;
    // 自适应高度
    [self.TitleLabel sizeToFit];
    
    self.WorksImageV = [[UIImageView alloc] init];
    self.WorksImageV.backgroundColor = [UIColor blackColor];
    [self.contentView addSubview:self.WorksImageV];
    // 打开用户交互
    self.WorksImageV.userInteractionEnabled = YES;
    
    self.TypeImageV = [[UIImageView alloc] init];
    self.TypeImageV.image = [UIImage imageNamed:@"tagicon"];
    [self.contentView addSubview:self.TypeImageV];
    
    self.TypeLabel = [[UILabel alloc] init];
    self.TypeLabel.backgroundColor = [UIColor clearColor];
    self.TypeLabel.textColor = DSRGBA(124, 124, 124, 1);
    [self.contentView addSubview:self.TypeLabel];
    // 点赞图片
    self.upButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.upButton setBackgroundImage:[UIImage imageNamed:@"mainCellDing"] forState:UIControlStateNormal];
    [self.upButton setBackgroundImage:[UIImage imageNamed:@"mainCellDingClick"] forState:UIControlStateHighlighted];
    [self.contentView addSubview:_upButton];
    [self.upButton addTarget:self action:@selector(upButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    // 不赞图片
    self.downButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.downButton setBackgroundImage:[UIImage imageNamed:@"mainCellCai"] forState:UIControlStateNormal];
    [self.downButton setBackgroundImage:[UIImage imageNamed:@"mainCellCaiClick"] forState:UIControlStateHighlighted];
    [self.contentView addSubview:_downButton];
    [self.downButton addTarget:self action:@selector(downButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    // 发送图片
    self.forwardButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.forwardButton setBackgroundImage:[UIImage imageNamed:@"mainCellShare"] forState:UIControlStateNormal];
    [self.forwardButton setBackgroundImage:[UIImage imageNamed:@"mainCellShareClick"] forState:UIControlStateHighlighted];
    [self.contentView addSubview:_forwardButton];
    [self.forwardButton addTarget:self action:@selector(forwardButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    // 评论
    self.commentButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.commentButton setBackgroundImage:[UIImage imageNamed:@"mainCellComment"] forState:UIControlStateNormal];
    [self.commentButton setBackgroundImage:[UIImage imageNamed:@"mainCellCommentClick"] forState:UIControlStateHighlighted];
    [self.contentView addSubview:_commentButton];
    [self.commentButton addTarget:self action:@selector(commentButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    
    self.upLabel = [[UILabel alloc] init];
    self.upLabel.backgroundColor = [UIColor clearColor];
    self.upLabel.textColor = DSRGBA(124, 124, 124, 1);
    [self.contentView addSubview:self.upLabel];
    
    self.downlabel = [[UILabel alloc] init];
    self.downlabel.backgroundColor = [UIColor clearColor];
    self.downlabel.textColor = DSRGBA(124, 124, 124, 1);
    [self.contentView addSubview:self.downlabel];
    
    self.forwardLabel = [[UILabel alloc] init];
    self.forwardLabel.backgroundColor = [UIColor clearColor];
    self.forwardLabel.textColor = DSRGBA(124, 124, 124, 1);
    [self.contentView addSubview:self.forwardLabel];
    
    self.commentLabel = [[UILabel alloc] init];
    self.commentLabel.backgroundColor = [UIColor clearColor];
    self.commentLabel.textColor = DSRGBA(124, 124, 124, 1);
    [self.contentView addSubview:self.commentLabel];
    
    self.bigView = [[BaseUIView alloc] init];
//    self.bigView.backgroundColor = DSRGBA(236, 236, 236, 1);
    [self.contentView addSubview:self.bigView];
    
    self.viewOne = [[BaseUIView alloc] init];
//    self.viewOne.backgroundColor = DSRGBA(236, 236, 236, 1);
    [self.contentView addSubview:self.viewOne];
    
    self.viewTwo = [[BaseUIView alloc] init];
//    self.viewTwo.backgroundColor = DSRGBA(236, 236, 236, 1);
    [self.contentView addSubview:self.viewTwo];
    
    self.viewThree = [[BaseUIView alloc] init];
//    self.viewThree.backgroundColor = DSRGBA(236, 236, 236, 1);
    [self.contentView addSubview:self.viewThree];
    
    self.crossView = [[BaseUIView alloc] init];
//    self.crossView.backgroundColor = DSRGBA(236, 236, 236, 1);
    [self.contentView addSubview:self.crossView];
    
    
    
    
    // 视频播放按钮
    self.VideoButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.VideoButton setImage:[UIImage imageNamed:@"bofang"] forState:UIControlStateNormal];
    // 播放按钮的点击
    [self.VideoButton addTarget:self action:@selector(VideoButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.WorksImageV addSubview:self.VideoButton];
    
    

}
-(void)layoutSubviews{
    [super layoutSubviews];
    
    self.bigView.frame = CGRectMake(0, 0, self.contentView.frame.size.width, 10);
    self.HeadImageV.frame = CGRectMake(10, 20, 30, 30);
    self.NameLabel.frame = CGRectMake(45, 20, 200, 15);
    self.TimeLabel.frame = CGRectMake(45, 35, 200, 15);
    // 计算TitleLabel的高度
    NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:17],NSFontAttributeName, nil];
    CGRect rect = [self.pictureModel.text boundingRectWithSize:CGSizeMake(self.frame.size.width, 0) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
    // TitleLabel的标题(自适应高度)
    self.TitleLabel.frame = CGRectMake(10, 70, self.contentView.frame.size.width - 20, rect.size.height);
    // 构造AVPlayerItem对象
    self.item = [[AVPlayerItem alloc] initWithURL:self.url];
    // 初始化player
    self.VideoPayer = [[AVPlayer alloc] initWithPlayerItem:self.item];
    // 需要一个AVPlayerLayer对象
    self.isLayer = [AVPlayerLayer playerLayerWithPlayer:self.VideoPayer];
    
    
    
    // 图片的自适应高度
    if ([_pictureModel.type isEqualToString:@"gif"]) {
        CGFloat gifHeight = (_pictureModel.gifModel.height.intValue) * (self.contentView.frame.size.width - 20) / (_pictureModel.gifModel.width.intValue);
        NSLog(@"%lf",gifHeight);
        self.WorksImageV.frame = CGRectMake(10, 80 + rect.size.height, self.contentView.frame.size.width - 20, gifHeight);
    
        self.VideoButton.frame = CGRectMake(0, 0, 0, 0);
        self.TypeImageV.frame = CGRectMake(10, 80 + rect.size.height + gifHeight + 10, 20, 20);
        self.TypeLabel.frame = CGRectMake(40, 90 + rect.size.height + gifHeight, 300, 20);
        self.crossView.frame = CGRectMake(0, 120 + rect.size.height + gifHeight, self.contentView.frame.size.width, 1);
        self.upButton.frame = CGRectMake(20, 130 + rect.size.height + gifHeight, 20, 20);
        // --------------------上下各自复印
        self.upLabel.frame = CGRectMake(40 + 7, 130 + rect.size.height + gifHeight, 40, 20);
        self.viewOne.frame = CGRectMake(self.contentView.frame.size.width / 4 * 1, 130 + rect.size.height + gifHeight, 1, 20);
        self.viewTwo.frame = CGRectMake(self.contentView.frame.size.width / 4 * 2, 130 + rect.size.height + gifHeight, 1, 20);
        self.viewThree.frame = CGRectMake(self.contentView.frame.size.width / 4 * 3, 130 + rect.size.height + gifHeight, 1, 20);
        self.downButton.frame = CGRectMake(self.contentView.frame.size.width / 4 * 1 + 20, 130 + rect.size.height + gifHeight, 20, 20);
        self.downlabel.frame = CGRectMake(self.contentView.frame.size.width / 4 * 1 + 40 + 7, 130 + rect.size.height + gifHeight, 40, 20);
        self.forwardButton.frame = CGRectMake(self.contentView.frame.size.width / 4 * 2 + 20, 130 + rect.size.height + gifHeight, 20, 20);
        self.forwardLabel.frame = CGRectMake(self.contentView.frame.size.width / 4 * 2 + 40 + 7, 130 + rect.size.height + gifHeight, 40, 20);
        self.commentButton.frame = CGRectMake(self.contentView.frame.size.width / 4 * 3 + 20, 130 + rect.size.height + gifHeight, 20, 20);
        self.commentLabel.frame = CGRectMake(self.contentView.frame.size.width / 4 * 3 + 40 + 7, 130 + rect.size.height + gifHeight, 40, 20);
        
        
        
        
        
    }else if ([_pictureModel.type isEqualToString:@"video"]){
        CGFloat videoHeight = (_pictureModel.videoModel.height.intValue) * (self.contentView.frame.size.width - 20) / (_pictureModel.videoModel.width.intValue);
        
        self.WorksImageV.frame = CGRectMake(10, 80 + rect.size.height, self.contentView.frame.size.width - 20, videoHeight);

        
        // 视频播放按钮坐标大小
        self.VideoButton.frame = CGRectMake(0, 0, 100, 100);
        self.VideoButton.center = CGPointMake(self.WorksImageV.frame.size.width / 2, self.WorksImageV.frame.size.height / 2);
        
        
        self.TypeImageV.frame = CGRectMake(10, 80 + rect.size.height + videoHeight + 10, 20, 20);
        self.TypeLabel.frame = CGRectMake(40, 90 + rect.size.height + videoHeight, 300, 20);
        self.crossView.frame = CGRectMake(0, 120 + rect.size.height + videoHeight, self.contentView.frame.size.width, 1);
        self.upButton.frame = CGRectMake(20, 130 + rect.size.height + videoHeight, 20, 20);
        // --------------------上下各自复印
        self.upLabel.frame = CGRectMake(40 + 7, 130 + rect.size.height + videoHeight, 40, 20);
        self.viewOne.frame = CGRectMake(self.contentView.frame.size.width / 4 * 1, 130 + rect.size.height + videoHeight, 1, 20);
        self.viewTwo.frame = CGRectMake(self.contentView.frame.size.width / 4 * 2, 130 + rect.size.height + videoHeight, 1, 20);
        self.viewThree.frame = CGRectMake(self.contentView.frame.size.width / 4 * 3, 130 + rect.size.height + videoHeight, 1, 20);
        self.downButton.frame = CGRectMake(self.contentView.frame.size.width / 4 * 1 + 20, 130 + rect.size.height + videoHeight, 20, 20);
        self.downlabel.frame = CGRectMake(self.contentView.frame.size.width / 4 * 1 + 40 + 7, 130 + rect.size.height + videoHeight, 40, 20);
        self.forwardButton.frame = CGRectMake(self.contentView.frame.size.width / 4 * 2 + 20, 130 + rect.size.height + videoHeight, 20, 20);
        self.forwardLabel.frame = CGRectMake(self.contentView.frame.size.width / 4 * 2 + 40 + 7, 130 + rect.size.height + videoHeight, 40, 20);
        self.commentButton.frame = CGRectMake(self.contentView.frame.size.width / 4 * 3 + 20, 130 + rect.size.height + videoHeight, 20, 20);
        self.commentLabel.frame = CGRectMake(self.contentView.frame.size.width / 4 * 3 + 40 + 7, 130 + rect.size.height + videoHeight, 40, 20);
        
        
        
    }else if([_pictureModel.type isEqualToString:@"image"]){
        CGFloat imageHeight = (_pictureModel.imageModel.height.intValue) * (self.contentView.frame.size.width - 20) / (_pictureModel.imageModel.width.intValue);
        
        self.WorksImageV.frame = CGRectMake(10, 80 + rect.size.height, self.contentView.frame.size.width - 20, imageHeight);
        
        self.VideoButton.frame = CGRectMake(0, 0, 0, 0);
        self.TypeImageV.frame = CGRectMake(10, 80 + rect.size.height + imageHeight + 10, 20, 20);
        self.TypeLabel.frame = CGRectMake(40, 90 + rect.size.height + imageHeight, 300, 20);
        self.crossView.frame = CGRectMake(0, 120 + rect.size.height + imageHeight, self.contentView.frame.size.width, 1);
        self.upButton.frame = CGRectMake(20, 130 + rect.size.height + imageHeight, 20, 20);
        // --------------------上下各自复印
        self.upLabel.frame = CGRectMake(40 + 7, 130 + rect.size.height + imageHeight, 40, 20);
        self.viewOne.frame = CGRectMake(self.contentView.frame.size.width / 4 * 1, 130 + rect.size.height + imageHeight, 1, 20);
        self.viewTwo.frame = CGRectMake(self.contentView.frame.size.width / 4 * 2, 130 + rect.size.height + imageHeight, 1, 20);
        self.viewThree.frame = CGRectMake(self.contentView.frame.size.width / 4 * 3, 130 + rect.size.height + imageHeight, 1, 20);
        self.downButton.frame = CGRectMake(self.contentView.frame.size.width / 4 * 1 + 20, 130 + rect.size.height + imageHeight, 20, 20);
        self.downlabel.frame = CGRectMake(self.contentView.frame.size.width / 4 * 1 + 40 + 7, 130 + rect.size.height + imageHeight, 40, 20);
        self.forwardButton.frame = CGRectMake(self.contentView.frame.size.width / 4 * 2 + 20, 130 + rect.size.height + imageHeight, 20, 20);
        self.forwardLabel.frame = CGRectMake(self.contentView.frame.size.width / 4 * 2 + 40 + 7, 130 + rect.size.height + imageHeight, 40, 20);
        self.commentButton.frame = CGRectMake(self.contentView.frame.size.width / 4 * 3 + 20, 130 + rect.size.height + imageHeight, 20, 20);
        self.commentLabel.frame = CGRectMake(self.contentView.frame.size.width / 4 * 3 + 40 + 7, 130 + rect.size.height + imageHeight, 40, 20);
        
    }
    
        
    
    
    
}

-(void)setPictureModel:(PictureModel *)pictureModel{
    NSMutableArray *nameArray = [NSMutableArray array];
    _pictureModel = pictureModel;

    self.NameLabel.text = _pictureModel.umodel.name;
    self.TimeLabel.text = _pictureModel.passtime;
    for (tagsModel *tagsM in _pictureModel.tags) {
        [nameArray addObject:tagsM.name];
    }
    NSString *str = [NSString stringWithFormat:@"%@",nameArray.firstObject];
    for (int i = 1; i < nameArray.count; i++) {
        str = [NSString stringWithFormat:@"%@  %@",str,nameArray[i]];
        
    }
    self.TypeLabel.text = str;
    

    
    self.upLabel.text = [NSString stringWithFormat:@"%@",_pictureModel.up];
    NSString *down = [NSString stringWithFormat:@"%@",_pictureModel.down];
    self.downlabel.text = down;
    self.forwardLabel.text = [NSString stringWithFormat:@"%@",_pictureModel.forward];
    self.commentLabel.text = [NSString stringWithFormat:@"%@",_pictureModel.comment];
    
    [self.HeadImageV sd_setImageWithURL:[NSURL URLWithString:_pictureModel.umodel.header.firstObject] placeholderImage:[UIImage imageNamed:@"tupian1.jpeg"]];
    
    self.TitleLabel.text = pictureModel.text;
    
    
    if ([_pictureModel.type isEqualToString:@"gif"]) {
        
    [self.WorksImageV sd_setImageWithURL:[NSURL URLWithString:(_pictureModel.gifModel.images.firstObject)] placeholderImage:[UIImage imageNamed:@"tupian1.jpeg"]];
    }else if ([_pictureModel.type isEqualToString:@"video"]) {
        
    [self.WorksImageV sd_setImageWithURL:[NSURL URLWithString:(_pictureModel.videoModel.thumbnail.firstObject)] placeholderImage:[UIImage imageNamed:@"tupian1.jpeg"]];
        // 创建URL
        self.url = [NSURL URLWithString:_pictureModel.videoModel.video.firstObject];
        
    }else{
    
    [self.WorksImageV sd_setImageWithURL:[NSURL URLWithString:(_pictureModel.imageModel.big.firstObject)] placeholderImage:[UIImage imageNamed:@"tupian1.jpeg"]];
        
    }

}

-(void)upButtonAction:(UIButton *)button{
    DSLog(@"我是精华推荐页(点赞按钮)点击方法");

}
-(void)downButtonAction:(UIButton *)button{
    DSLog(@"我是精华推荐页(不赞按钮)点击方法");

}
-(void)forwardButtonAction:(UIButton *)button{
    DSLog(@"我是精华推荐页(发送按钮)点击方法");
    
}
-(void)commentButtonAction:(UIButton *)button{
    DSLog(@"我是精华推荐页(评论按钮)点击方法");
    
    
}
-(void)VideoButtonAction:(UIButton *)button{
    DSLog(@"我是视频播放按钮的点击方法");
    self.isPlay = YES;
        self.WorksImageV.image = nil;
        if (self.VideoButton.selected == 0) {
            // 播放窗口坐标大小
            self.isLayer.frame = CGRectMake(0, 0, self.WorksImageV.frame.size.width, self.WorksImageV.frame.size.height);
            
            self.isLayer.videoGravity = AVLayerVideoGravityResizeAspect;
            
            //添加到屏幕上显示(插入)
            [self.WorksImageV.layer insertSublayer:self.isLayer atIndex:0];
            
            [self.VideoPayer play];
            [self.VideoButton setImage:[UIImage imageNamed:@""] forState:UIControlStateNormal];
            
        }else if(self.VideoButton.selected){
            // 视频暂停播放
            [self.VideoPayer pause];
            
            [self.VideoButton setImage:[UIImage imageNamed:@"bofang"] forState:UIControlStateNormal];
            
        }
        self.VideoButton.selected = !self.VideoButton.selected;
    
    
}



- (void)setIsPlay:(BOOL)isPlay {
    _isPlay = isPlay;
    if (!isPlay) {
        // 停止播放;
        _VideoPayer.rate = 0;
        //删除播放器的layer层
        [self.isLayer removeFromSuperlayer];
        self.VideoButton.selected = NO;
        [self.VideoButton setImage:[UIImage imageNamed:@"bofang"] forState:UIControlStateNormal];

        
    }
}












@end
